<template>
  <div class="member-level-operation">
    <operation-title
      title="会员等级"
      :icon="TitleIcon"
    />
    <div class="divider"></div>
    <div class="func">
      <p class="attr">选择风格</p>
      <div class="adjust-item">
        <div class="style-btn" @click=" dialogFormVisible = true">修改风格</div>
        <span class="style-word">当前风格{{ pageConfig.style }}</span>
      </div>
    </div>

    <div class="func">
      <p class="attr">等级配置</p>
      <div class="adjust-item">
        <span>组件背景</span>
        <el-radio-group v-model="config.pageConfig.cptType">
          <el-radio label="1">背景色</el-radio>
          <el-radio label="2">背景图</el-radio>
        </el-radio-group>
      </div>
      <div class="adjust-item" v-show="config.pageConfig.cptType == 1">
        <span></span>
        <el-color-picker v-model="config.pageConfig.bgColor" />
        <span class="color-value">{{ config.pageConfig.bgColor }}</span>
      </div>
      <div class="add-item-input" v-show="config.pageConfig.cptType == 2">
        <p></p>
        <h4>建议图片宽度355px，高度120px</h4>
        <div class="upload-img" v-if="config.pageConfig.bgImg">
          <el-image style="width: 100%; height: 100%" :src="config.pageConfig.bgImg" fit="contain"></el-image>
          <div class="upload-img_cover">
            <Upload :model-value="fileList" file-path="act/" :limit="999" :show-file-list="false"
              @success="uploadSuccess('bgImg', $event)">
              <el-button size="small" @click="currentType = 'bgImg'">替换</el-button>
            </Upload>
          </div>
        </div>
        <Upload v-else v-model="config.pageConfig.bgImg" file-path="act/" list-type="picture-card">
          <el-icon>
            <Plus />
          </el-icon>
        </Upload>
      </div>
      <!-- 风格1昵称设置 -->
      <div class="adjust-item" v-if="pageConfig.style == 1">
        <span>昵称颜色</span>
        <el-color-picker v-model="config.pageConfig.nameColor" />
        <span class="color-value">{{ config.pageConfig.nameColor }}</span>
      </div>

      <!-- 风格2中轨道设置 -->
      <div v-if="pageConfig.style == 2">
        <div class="adjust-item"> <span>轨道颜色</span>
          <el-color-picker v-model="config.pageConfig.trackColor" />
          <span class="color-value">{{ config.pageConfig.trackColor }}</span>
        </div>
        <div class="adjust-item"> <span>轨道等级默认颜色</span>
          <el-color-picker v-model="config.pageConfig.trackLevelDefaultColor" />
          <span class="color-value">{{ config.pageConfig.trackLevelDefaultColor }}</span>
        </div>
        <div class="adjust-item"> <span>轨道等级选中颜色</span>
          <el-color-picker v-model="config.pageConfig.trackLevelSelectedColor" />
          <span class="color-value">{{ config.pageConfig.trackLevelSelectedColor }}</span>
        </div>
        <div class="adjust-item"> <span>成长值颜色</span>
          <el-color-picker v-model="config.pageConfig.grouthValueColor" />
          <span class="color-value">{{ config.pageConfig.grouthValueColor }}</span>
        </div>
        <div class="adjust-item"> <span>成长值大小</span>
          <el-slider v-model="config.pageConfig.grouthValueFontSize" :min="0" :max="50" show-input size="small" />px
        </div>
      </div>


      <div class="level-list" v-show="memberLevelList.length">
        <div class="level-item" v-for="(member, index) in memberLevelList">
          <div class="level-name">{{ member.memberName }}</div>
          <div class="item-content">
            <div class="item-row"> <span class="subtitle">背景图</span>
              <div class="upload-img" v-if="member.bgImg">
                <el-image style="width: 100%; height: 100%" :src="member.bgImg" fit="contain"></el-image>
                <div class="upload-img_cover">
                  <Upload :model-value="fileList" file-path="act/" :limit="999" :show-file-list="false"
                    @success="uploadSuccess('memberImg', $event, index)">
                    <el-button size="small" @click="currentType = 'memberImg'">替换</el-button>
                  </Upload>
                </div>
              </div>
              <Upload v-else v-model="member.bgImg" file-path="act/" list-type="picture-card">
                <el-icon>
                  <Plus />
                </el-icon>
              </Upload>
              <span class="img-tip">建议尺寸272*152</span>
            </div>
            <div class="item-row">
              <span class="subtitle">文字颜色</span>
              <el-color-picker v-model="member.textColor" />
              <span class="color-value">{{ member.textColor }}</span>
            </div>
            <div class="item-row">
              <span class="subtitle">升级按钮</span>
              <el-switch v-model="member.showUpgradeBtn" />
            </div>
            <div class="item-row" v-if="member.showUpgradeBtn">
              <span class="subtitle"></span>
              <div class="upload-img" v-if="member.upgradeBtnImg">
                <el-image style="width: 100%; height: 100%" :src="member.upgradeBtnImg" fit="contain"></el-image>
                <div class="upload-img_cover">
                  <Upload :model-value="fileList" file-path="act/" :limit="999" :show-file-list="false"
                    @success="uploadSuccess('upgradeBtnImg', $event, index)">
                    <el-button size="small" @click="currentType = 'upgradeBtnImg'">替换</el-button>
                  </Upload>
                </div>
              </div>
              <Upload v-else v-model="member.upgradeBtnImg" file-path="act/" list-type="picture-card">
                <el-icon>
                  <Plus />
                </el-icon>
              </Upload>
              <span class="img-tip">建议尺寸60*20</span>
            </div>
            <div class="item-row" v-if="member.showUpgradeBtn">
              <span class="subtitle"></span>
              <div class="link-div" @click="openLink(member, index)">
                <el-icon>
                  <Link />
                </el-icon>
                <span style="width: 100%" v-if="member.upgradeUrl">{{
                  member.upgradeUrlName
                }}</span>
                <span v-else style="width: 80px">请选择链接</span>
              </div>
            </div>
            <div class="item-row">
              <span class="subtitle">升级弹窗</span> <el-switch v-model="member.showUpgradeDialog" />
            </div>
            <div class="item-row" v-if="member.showUpgradeDialog">
              <span class="subtitle"></span>
              <div class="upload-img" v-if="member.upgradeDialogImg">
                <el-image style="width: 100%; height: 100%" :src="member.upgradeDialogImg" fit="contain"></el-image>
                <div class="upload-img_cover">
                  <Upload :model-value="fileList" file-path="act/" :limit="999" :show-file-list="false"
                    @success="uploadSuccess('upgradeDialogImg', $event, index)">
                    <el-button size="small" @click="currentType = 'upgradeDialogImg'">替换</el-button>
                  </Upload>
                </div>
              </div>
              <Upload v-else v-model="member.upgradeDialogImg" file-path="act/" list-type="picture-card">
                <el-icon>
                  <Plus />
                </el-icon>
              </Upload>
              <span class="img-tip">建议尺寸318*312</span>
            </div>
          </div>
        </div>
      </div>

    </div>
    <!-- 边距圆角设置 -->
    <div>
      <div class="divider"></div>
      <div class="func">
        <p class="attr">组件边距</p>
        <div class="adjust-item">
          <span>上边距</span>
          <el-slider v-model="config.pageConfig.marginTop" :min="0" :max="50" show-input size="small" />px
        </div>
        <div class="adjust-item">
          <span>下边距</span>
          <el-slider v-model="config.pageConfig.marginBottom" :min="0" :max="50" show-input size="small" />px
        </div>
        <div class="adjust-item">
          <span>左右边距</span>
          <el-slider v-model="config.pageConfig.margin" :min="0" :max="50" show-input size="small" />px
        </div>
      </div>
      <div class="divider"></div>
      <div class="func">
        <p class="attr">圆角设置</p>
        <div class="adjust-item">
          <span>上圆角</span>
          <el-slider :min="0" :max="20" v-model="config.pageConfig.radiusTop" show-input size="small" />px
        </div>
        <div class="adjust-item">
          <span>下圆角</span>
          <el-slider :min="0" :max="20" v-model="config.pageConfig.radiusBottom" show-input size="small" />px
        </div>
      </div>
    </div>

    <Dialog class="dialog-form" v-model="dialogFormVisible" title="选择风格" width="60%" :before-close="resetForm">
      <div class="style-div-container">
        <div class="style-div" v-for="(item, index) in styleList" :key="index" @click="submitForm(item.id)">
          <img :src='item.png' />
          <span> {{ `风格${item.id}` }} </span>
        </div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="resetForm">取消</el-button>
        </span>
      </template>
    </Dialog>

    <href-selector v-model:show="linkFormVisible" :model-value="linkObject" @confirm="confirmLink" />

  </div>
</template>
<script lang="ts">

export default {
  name: "MemberLevelOperation",
};
</script>

<script setup lang="ts">

import { useHrefSelector } from "@/views/activity/actApp/components/default/compositions/useHrefSelector";
import { getVipRank } from "@/api/vipCenter";

import level1png from '@/assets/app/other/style/member-level1.png'
import level2png from '@/assets/app/other/style/member-level2.png'
import TitleIcon from "@/assets/app/icon/memberlevel.png";
import OperationTitle
  from "@/views/activity/actApp/components/default/operationComponents/operationTitle/OperationTitle.vue";


const config = defineProps({
  pageConfig: {
    type: Object,
    default: {
      style: 1, // 风格
      bgColor: "#FFFFFF",//组件背景色
      cptType: "2", // 组件背景类型
      bgImg: '',//组件背景图片
      //风格2
      trackColor: '',//轨道颜色
      trackLevelDefaultColor: '',//轨道等级默认颜色
      trackLevelSelectedColor: '',//轨道等级选中颜色
      grouthValueColor: "",//成长值颜色
      grouthValueFontSize: 20,//成长值字号

      //风格1 边距
      nameColor: "", //昵称颜色
      marginTop: 10,
      marginBottom: 10,
      margin: 10,
      paddingTop: 16,
      paddingLeft: 24,
      paddingRight: 12,
      radiusTop: 8,
      radiusBottom: 8,

      //会员相关等级列表
      memberLevelList: []
    },
  },
});

const { linkFormVisible, linkIndex, linkObject, submitUrl, linkDialog } =
  useHrefSelector();
let emits = defineEmits<{ (e: "changeStyle", configStyle: Object): void }>();
const currentType = ref('');

const dialogFormVisible = ref(false)
const styleList = ref([{ id: '1', png: level1png }, { id: '2', png: level2png }])

const memberLevelList = ref([])

const fileList = ref([]);
const uploadSuccess = (type: string, e: any, index?: number) => {
  if (type == 'bgImg') {
    config.pageConfig[type] = e.url

  } else
    if (type == 'memberImg') {
      config.pageConfig.memberLevelList[index].bgImg = e.url

    } else {
      config.pageConfig.memberLevelList[index][type] = e.url

    }
  fileList.value = []
};

let currentLinkIndex = undefined

const openLink = (item, index) => {
  linkDialog(item, 0)
  currentLinkIndex = index
}

const submitForm = (item: number) => {
  config.pageConfig.style = item;
  dialogFormVisible.value = false;
};

watch(config.pageConfig, (val) => {
  emits("changeStyle", config.pageConfig);

});


watch(memberLevelList, (val) => {
  const data = config.pageConfig
  data.memberLevelList = memberLevelList.value
  emits("changeStyle", data);
});

const resetForm = () => {
  dialogFormVisible.value = false;
}

const confirmLink = (link: any) => {
  memberLevelList.value[currentLinkIndex].upgradeUrl = link.url
  memberLevelList.value[currentLinkIndex].upgradeUrlName = link.urlName
  currentLinkIndex = undefined
};

//获取会员等级列表
const getMemberList = async () => {
  const res = await getVipRank()
  if (res.code == 200) {
    if (res.payload?.length) {
      memberLevelList.value = res.payload.map(item => ({
        ...item,
        bgImg: '',
        showUpgradeBtn: false,
        upgradeBtnImg: '',
        upgradeUrl: "",
        upgradeUrlName: '',
        showUpgradeDialog: false,
        upgradeDialogImg: "",
        textColor: '#E27757',

      }))
    }

  }
}


watch(config.pageConfig.memberLevelList, (val) => {
  if (val.length > 0) {
    memberLevelList.value = config.pageConfig.memberLevelList
  } else {
    getMemberList()
  }
}, {
  immediate: true
})

</script>
<style lang="scss" scoped>
.style-div-container {
  display: flex;
  flex-direction: row;

  .style-div {
    margin: 12px;
    background: #fff;
    text-align: center;

    img {
      display: block;
      max-width: 100%;
      max-height: 100%;
      margin-bottom: 10px;
    }
  }
}

.member-level-operation {
  .title {
    padding: 8px 23px;
    overflow: hidden;
    border-bottom: 5px solid #f6f7f9;
    display: flex;
    position: relative;
    height: 50px;
    box-sizing: border-box;

    img {
      position: absolute;
      left: 0;
      top: -5px;
    }

    h5 {
      font-size: 16px;
      font-weight: 700;
      position: absolute;
      left: 70px;
      top: 0;
      line-height: 50px;
    }
  }

  .func {
    border-bottom: 5px solid #f6f7f9;
    padding: 20px 16px;
    font-size: 16px;

    .color-value {
      display: inline-block;
      width: 200px;
      text-align: left;
      margin-left: 5px;
    }

    .attr {
      line-height: 1;
      color: #47565d;
      font-weight: bolder;
      margin-bottom: 20px;
    }

    ul.common-ul {
      list-style: none;
      display: flex;
      align-items: center;
      justify-content: center;

      li {
        flex: 1;
        height: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 4px 0 0 4px;
        border: 2px solid #ededed;
        border-collapse: collapse;

        &.active {
          border: 2px solid #fb6638;
        }

        img {
          max-width: 100% !important;
          max-height: 100% !important;
        }
      }
    }

    h4 {
      position: relative;
      padding: 8px 16px 8px 16px;
      border-radius: 6px;
      color: red;
      font-size: 16px;
      font-weight: 600;
      line-height: 22px;
      margin-bottom: 10px;
      border: 1px solid #d8e8fc;
      background-color: #e5f0fe;
      width: 170px;
      margin-right: 10px;
      margin-top: 10px;
    }

    div.adjust-item {
      display: flex;
      align-items: center;
      margin-top: 3px;
      margin-bottom: 10px;

      span {
        width: 80px;
        text-align: right;
        margin-right: 15px;
        color: #666;
      }

      :deep(.el-slider) {
        width: 240px;
        margin-right: 3px;
        margin-left: 15px;
      }

      :deep(.el-slider__runway.show-input) {
        margin-right: 15px;
      }

      :deep(.el-input-number) {
        width: 100px;
        background: #f4f3f7;
        border: 1px solid #f4f3f7;
      }

      .style-btn {
        width: 240px;
        height: 36px;
        font-size: 12px;
        line-height: 36px;
        text-align: center;
        background-color: #001E70;
        color: #fff;
        margin-right: 5px;
        cursor: pointer;
      }

      .style-word {
        font-size: 12px;
      }
    }


  }

  .add-item-input {
    display: flex;
    align-items: center;
    margin-bottom: 20px;

    >p {
      width: 77px;
      text-align: right;
      margin-right: 8px;
    }

    .upload-img {
      width: 62px;
      height: 62px;
      margin-right: 10px;
      position: relative;

      :deep(.el-image) {
        width: 62px;
        height: 62px;
      }

      &:hover {
        .upload-img_cover {
          opacity: 1;
        }
      }

      .upload-img_cover {
        opacity: 0;
        position: absolute;
        transition: all 0.3s ease;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background-color: rgba(0, 0, 0, 0.7);
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: space-evenly;
      }
    }

    :deep(.el-upload-list) {
      width: 62px;
      height: 62px;
      margin-right: 10px;
    }

    :deep(.el-upload--picture-card) {
      width: 62px;
      height: 62px;
    }

    // img {
    //   width: 40px;
    //   height: 40px;
    // }
    :deep(.el-color-picker) {
      margin-right: 10px;
    }


  }

  .level-list {
    padding: 0 10px;

    .level-item {
      padding: 15px;
      margin: 10px 0;
      border-radius: 8px;
      background: #f2f2f2;

      .level-name {
        font-size: 14px;
        font-weight: 700;
      }

      .item-content {
        // padding: 10px;

        .item-row {
          display: flex;
          align-items: center;
          margin-bottom: 10px;
          font-size: 14px;

          .subtitle {
            width: 70px;
            text-align: right;
            margin-right: 15px;
          }

          .upload-img {
            width: 62px;
            height: 62px;

            margin-right: 10px;
            position: relative;

            :deep(.el-image) {
              width: 62px;
              height: 62px;
            }

            &:hover {
              .upload-img_cover {
                opacity: 1;
              }
            }

            .upload-img_cover {
              opacity: 0;
              position: absolute;
              transition: all 0.3s ease;
              top: 0;
              left: 0;
              width: 100%;
              height: 100%;
              background-color: rgba(0, 0, 0, 0.7);
              display: flex;
              flex-direction: column;
              align-items: center;
              justify-content: space-evenly;
            }
          }

          .img-tip {
            // margin-top: 30px;
          }

          :deep(.el-upload-list) {
            width: 62px;
            height: 62px;
            margin-right: 10px;
          }

          :deep(.el-upload--picture-card) {
            width: 62px;
            height: 62px;
          }

          .link-div {
            width: calc(100% - 80px);
            display: flex;
            // margin-left: 84px;
            align-items: center;
            background: #e8effc;
            border: 0;
            padding: 8px 0 8px 16px;
            cursor: pointer;
            position: relative;

            :deep(.el-icon) {
              margin-right: 10px;
            }

            :deep(.el-input) {
              font-size: 12px;
              height: 30px;
              width: calc(100% - 30px);
            }

            .close-btn {
              position: absolute;
              right: 5px;
            }
          }


          // .upload-img {
          //   width: 62px;
          //   height: 62px;
          //   margin-right: 10px;
          //   // position: relative;
          // }
        }
      }


    }
  }

}
</style>
